//
//  rTwoViewController.m
//  Item A House
//
//  Created by 甄奇文 on 15/5/27.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "rTwoViewController.h"
#import "postModel.h"
#import "RentalModel.h"
#import "BasicTableViewCell.h"
#import "DetailTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "RentalMapModel.h"
#import "MapTableViewCell.h"

#import <UIImageView+WebCache.h>

#import "RentalCollectionModel.h"

#import "RentalMapModel.h"
#import "rMapViewController.h"
@interface rTwoViewController ()<UITableViewDataSource, UITableViewDelegate, postModelDelegate>
@property (nonatomic, retain) postModel *conn;
@end

@implementation rTwoViewController

- (void)dealloc
{
     _conn.delegate = nil;
    [_collectionButton release];
    [_conn release];
    [_table release];
    [_scroll release];
    [_photoArray release];
    [_array release];
    [_zfmapArray release];
    [super dealloc];
}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.photoArray = [NSMutableArray array];
        self.zfmapArray = [NSMutableArray array];
    }
    return self;
}

- (void)connectFinished:(NSData *)data
{
    NSError *error = nil;
    NSMutableDictionary *dic  = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSMutableArray *array = [NSMutableArray array];
    self.photoArray = [dic objectForKey:@"zfphoto"];
    array = [dic objectForKey:@"zfinfo"];
    for (NSMutableDictionary *dic in array) {
        RentalModel *model = [[RentalModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.array addObject:model];
    }
    NSMutableArray *arr = [dic objectForKey:@"zfmap"];
    for (NSMutableDictionary *dic in arr) {
        RentalMapModel *map = [[RentalMapModel alloc] init];
        [map setValuesForKeysWithDictionary:dic];
        [self.zfmapArray addObject:map];
    }
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 180)];
    self.scroll.bounces = NO;
    self.scroll.pagingEnabled = YES;
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * [self.photoArray count], 0);
    for (int i = 0; i < [self.photoArray count]; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(self.scroll.frame.size.width * i, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
        image.userInteractionEnabled = YES;
        NSDictionary *dic = [self.photoArray objectAtIndex:i];
        NSString *str = [NSString stringWithFormat:@"%@%@", [dic objectForKey:@"picpath"], [dic objectForKey:@"picname"]];
        [image sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"10.jpg"]];
        [self.scroll addSubview:image];
    }
    if ([self.photoArray count] == 0) {
        UIImageView *photo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
        [photo setImage:[UIImage imageNamed:@"0.jpg"]];
        [self.scroll addSubview:photo];
        [photo release];
    }
    [self.scroll release];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.table.tableHeaderView = self.scroll;
    self.table.rowHeight = [UIScreen mainScreen].bounds.size.height / 3;
    //self.table.allowsSelection = NO;
    self.table.dataSource = self;
    self.table.delegate = self;
    [self.view addSubview:self.table];
    [self.table release];
    
}

- (void)newAction:(UITapGestureRecognizer *)tap
{
    rMapViewController *map = [[rMapViewController alloc] init];
    [self.navigationController pushViewController:map animated:YES];
    [map release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 300;
    } else if (indexPath.section == 1) {
        return 100;
    } else {
        return 180;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return 1;
    }
    if (section == 2) {
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
            static NSString *cellIdentify = @"lgd";
            BasicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
            if (!cell) {
                cell = [[[BasicTableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:cellIdentify]autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
            }
            if ([self.array count] == 0) {
                return cell;
            } else {
                RentalModel *model = [self.array objectAtIndex:indexPath.row];
                cell.title.text = model.title;
                cell.v_price.text = [NSString stringWithFormat:@"%ld万", (long)model.v_price];
                cell.v_houselayout.text = model.v_houselayout;
                cell.v_housesize.text = model.v_housesize;
                cell.addrinfo.text = model.addrinfo;
                cell.areainfo.text = [NSString stringWithFormat:@"(%@,", model.areainfo];
                cell.smallareaid.text = [NSString stringWithFormat:@"%@)", model.smallareaid];
                cell.countexpress.text = [NSString stringWithFormat:@"付租:%@", model.countexpress];
                cell.houseward.text = [NSString stringWithFormat:@"朝向:%@", model.houseward];
                cell.v_floor.text = [NSString stringWithFormat:@"楼层:%@", model.v_floor];
                cell.fitment.text = [NSString stringWithFormat:@"装修:%@", model.fitment];
                cell.v_phone.text = [NSString stringWithFormat:@"   ☎️ 联系房产顾问:%@", model.v_phone];
                self.collectionButton.enabled = YES;
                return  cell;
            }
        }
    
    if (indexPath.section == 1) {
        static NSString *cellIdentify = @"kkk";
        DetailTableViewCell *detail = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (!detail) {
            detail = [[[DetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]autorelease];
            detail.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if ([self.array count] == 0) {
            return detail;
        } else {
            RentalModel *model2 = [self.array objectAtIndex:indexPath.row];
            detail.housefeature.text = [NSString stringWithFormat:@"      %@", model2.housefeature];
            return detail;
        }
    }
    
if (indexPath.section == 2) {
    static NSString *cellIdentify = @"fff";
    MapTableViewCell *map = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    
    if (!map) {
        map = [[[MapTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]autorelease];
        map.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    
    
    if ([self.zfmapArray count] == 0) {
        return map;
    } else {
        RentalMapModel *model3 = [self.zfmapArray objectAtIndex:indexPath.row];
//     [map.mapimg sd_setImageWithURL:[NSURL URLWithString:model3.mapimg] placeholderImage:[UIImage imageNamed:@"10.jpg"]];
      map.address.text = [NSString stringWithFormat:@"地址:%@", model3.address];
        map.way.text = [NSString stringWithFormat:@"公交:%@", model3.way];
        
        UITapGestureRecognizer *recognMap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newAction:)];
        [map.rMap addGestureRecognizer:recognMap];
        
        [recognMap release];
        
        return map;
    }
}
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"基本信息";
    }
    if (section == 1) {
        return @"详细描述";
    }
    if (section == 2) {
        return @"房屋周边";
    }
    return nil;
}
//http://m.goufang.com/api/zufang/zfinfo
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createButton];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSLog(@"%@", self.hid_id);
    [dic setObject:@"dl" forKey:@"cityid"];
    [dic setObject:self.hid_id forKey:@"hid"];

    self.conn = [postModel connectWithUrl:@"http://m.goufang.com/api/zufang/zfinfo" delegate:self parmaters:dic];


   
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(15, 5, 38, 38);
    
    [btn setBackgroundImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    
    [btn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    
    UIBarButtonItem*back=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=back;
}
- (void)createButton
{
    // 收藏
    self.collectionButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIImage *image = [UIImage imageNamed:@"My.png"];
    [self.collectionButton setImage:image forState:UIControlStateNormal];
    [self.collectionButton addTarget:self action:@selector(collectionButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.collectionButton];
    self.collectionButton.enabled = NO;
    
    [self.navigationItem setHidesBackButton:YES];
}
-(void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)collectionButton:(id)sender
{
    RentalModel *model = [self.array lastObject];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:model.houseid forKey:@"houseid"];
    [dic setObject:model.title forKey:@"title"];
    [RentalCollectionModel writeArray:dic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
@end
